package com.training1.core.web.controller;

import java.io.BufferedOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.global.signIn.LoginInfo;
import com.training1.core.common.global.signIn.LoginInfoUtil;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.entity.BatchWithDrawalEntity;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.RemitService;
@Controller
@RequestMapping("remit")
public class RemitController extends BaseController {
	@Resource
	private RemitService service;
	
	@RequestMapping({ "/", "" })
	public String auditSet() {
		return "yixiang/remitSetting";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page<BatchWithDrawalEntity> list(String cellphone, String createTimeFrom, String createTimeTo,
			String withdrawCashFrom, String withdrawCashTo, Page<BatchWithDrawalEntity> page) {
		Map<String,Object> map = new HashMap<String,Object>();
		page.getSearch().put("cellphone", cellphone);
		map.put("cellphone", cellphone);
		if (createTimeFrom != null) {
			page.getSearch().put("createTimeFrom", createTimeFrom + " 00:00:00");
			map.put("createTimeFrom", createTimeFrom + " 00:00:00");
		}
		if (createTimeTo != null) {
			page.getSearch().put("createTimeTo", createTimeTo + " 23:59:59");
			map.put("createTimeTo", createTimeTo + " 23:59:59");
		}
			page.getSearch().put("withdrawCashFrom", withdrawCashFrom);
			page.getSearch().put("withdrawCashTo", withdrawCashTo);
			map.put("withdrawCashFrom", withdrawCashFrom);
			map.put("withdrawCashTo", withdrawCashTo);
		//通过条件查找
		service.findList(page.getSearch());
		//查询金额总和
		BatchWithDrawalEntity bde = service.findAllMoney(map);
		if(bde==null){
			bde = new BatchWithDrawalEntity();
		}
		bde.setBankforCard("总合计：");
		List<BatchWithDrawalEntity> cashes = new ArrayList<BatchWithDrawalEntity>();
		cashes.add(bde);
		//设置页面footer,显示总合计
		page.setFooter(cashes);

		return page;
	}
	
	@RequestMapping(value = "/remitSuccess", method = RequestMethod.POST)
	@ResponseBody
	public RespJson remitSuccess(String orderNumber) {
		LoginInfo info = LoginInfoUtil.getLoginInfo();
		String updateName = info.getLoginName();
		Map<String, Object> map = saveMap(orderNumber, updateName);
		if(service.updateStatus(map)>0){
			return RespJsonFactory.buildSuccess();
		}else{
			return RespJsonFactory.buildFailure();
		}

	}
	// 审核时将数据存到map
	public Map<String, Object> saveMap(String orderNumber, String updateName) {
		//将orderNumber按，切割成数组
		String[] str = orderNumber.split(",");
		//定义map集合用来存放orderNumber
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> list = new ArrayList<String>();
		if (str.length == 1) {
			list.add(orderNumber);
			map.put("orderNumber", list);
			map.put("auditor", updateName);
		} else {

			for (int i = 0; i < str.length; i++) {
				list.add(str[i]);
			}
			map.put("orderNumber", list);
			map.put("auditor", updateName);
		}
		return map;
	}
	
	@RequestMapping("/export")
	// @ResponseBody
	public String outExcel(String cellphone, String createTimeFrom, String createTimeTo, String withdrawCashFrom,
			String withdrawCashTo, HttpServletResponse resp) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("cellphone", cellphone);
		if(createTimeFrom!=null){
			map.put("createTimeFrom", createTimeFrom+" 00:00:00");
		}
		if(createTimeTo!=null){
			map.put("createTimeTo", createTimeTo+" 23:59:59");
		}
		map.put("withdrawCashFrom", withdrawCashFrom);
		map.put("withdrawCashTo", withdrawCashTo);
		//根据条件查询
		List<BatchWithDrawalEntity> list = service.findList(map);
		service.export(list,resp);
		return null;
	}
}
